package com.google.code.fcr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Rohan Ranade
 */
public class CardTableModel extends AbstractTableModel {

    private final List<Card> cards = new ArrayList<Card>();

    public CardTableModel() {
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "Name";
            case 1: return "Hint";
            case 2: return "Description";
        }
        assert true;
        return null;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public int getRowCount() {
        return cards.size();
    }

    public int getColumnCount() {
        return 3;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        String retValue = null;
        Card card = cards.get(rowIndex);
        switch (columnIndex) {
            case 0:
                retValue = card.getName();
                break;
            case 1:
                retValue = card.getHint();
                break;
            case 2:
                retValue = card.getDescription();
                break;
        }
        return retValue;
    }

    public void appendCard(Card card) {
        int idx = cards.size();
        cards.add(card);
        fireTableRowsInserted(idx, idx);
    }

    public Card getCardAt(int rowIndex) {
        return cards.get(rowIndex);
    }

    public void setCardAt(Card card, int rowIndex) {
        cards.set(rowIndex, card);
        fireTableRowsUpdated(rowIndex, rowIndex);
    }

    public void removeCard(int index) {
        cards.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void moveUp(int rowIndex) {
        assert rowIndex > 0;
        Card temp = cards.get(rowIndex);
        Card temp2 = cards.get(rowIndex - 1);
        cards.set(rowIndex, temp2);
        cards.set(rowIndex - 1, temp);
        fireTableRowsUpdated(rowIndex - 1, rowIndex);
    }

    public void moveDown(int rowIndex) {
        assert rowIndex < cards.size() - 1;
        Card temp = cards.get(rowIndex);
        Card temp2 = cards.get(rowIndex + 1);
        cards.set(rowIndex, temp2);
        cards.set(rowIndex + 1, temp);
        fireTableRowsUpdated(rowIndex, rowIndex + 1);
    }

    public List<Card> getCards() {
        return Collections.unmodifiableList(cards);
    }
}
